package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 满超
 * @date 2020/06/25
 **/
@Configuration
public class RabbitMqConfig {

    //配置序列化类型
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 注入mq配置,自动创建
     */

    @Bean
    public Exchange stockExchange() {
        return new TopicExchange("stock-event-exchange",true,false);
    }

    @Bean
    public Queue stockQueue() {
        return new Queue("stock.release.stock.queue",true,false,false);
    }

    @Bean
    public Queue stockDelayQueue() {
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","stock-event-exchange");//死信交换机
        map.put("x-dead-letter-routing-key","stock.release");//死信路由key
        map.put("x-message-ttl",120000);//120秒后收到
        return new Queue("stock.delay.queue",true,false,false,map);
    }

    //普通绑定
    @Bean
    public Binding stockBind(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null);
    }

    //死信队列绑定
    @Bean
    public Binding stockDelayBind(){
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null);
    }
}
